package cn.kooki.app.duobao.ui.Activity.Share;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.data.Bean.share.ShareListItem;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.data.constant.Constants;
import cn.kooki.app.duobao.ui.Activity.Comm.RootAcitivity;
import cn.kooki.app.duobao.ui.Adapter.ListImageAdapter;
import cn.kooki.app.duobao.ui.Adapter.ShareDialogAdapter;
import cn.kooki.app.duobao.ui.widget.ActionBarMenu;
import cn.kooki.app.duobao.ui.widget.CustomListView;
import cn.kooki.app.duobao.util.ScreenUtils;
import cn.kooki.app.duobao.util.ShareUtils;
import cn.kooki.app.duobao.util.TimeUtils;
import cn.kooki.app.duobao.util.manager.AppManager;
import cn.kooki.app.duobao.util.manager.ShopCartManager;
import de.greenrobot.event.EventBus;

/**
 * Created by xiong on 2015/12/19.
 */
public class ShareDetailActivity extends BaseNavActivity implements View.OnClickListener {
    @Bind(R.id.share_detail_title)
    TextView shareDetailTitle;
    @Bind(R.id.share_detail_time)
    TextView shareDetailTime;
    @Bind(R.id.share_detail_author)
    TextView shareDetailAuthor;
    @Bind(R.id.one_title_1)
    TextView oneTitle1;
    @Bind(R.id.share_detail_goods)
    TextView shareDetailGoods;
    @Bind(R.id.one_title_period)
    TextView oneTitlePeriod;
    @Bind(R.id.one_title_period_text)
    TextView oneTitlePeriodText;
    @Bind(R.id.one_title_2)
    TextView oneTitle2;
    @Bind(R.id.share_detail_num)
    TextView shareDetailNum;
    @Bind(R.id.one_title_3)
    TextView oneTitle3;
    @Bind(R.id.share_detail_code)
    TextView shareDetailCode;
    @Bind(R.id.one_title_4)
    TextView oneTitle4;
    @Bind(R.id.share_detail_reveal_time)
    TextView shareDetailRevealTime;
    @Bind(R.id.share_detail_goods_wrapper)
    RelativeLayout shareDetailGoodsWrapper;
    @Bind(R.id.share_detail_content)
    TextView shareDetailContent;
    @Bind(R.id.share_detail_textWrapper)
    LinearLayout shareDetailTextWrapper;
    @Bind(R.id.share_detail_image_list)
    CustomListView shareDetailImageList;
    @Bind(R.id.share_detail_root)
    LinearLayout shareDetailRoot;
    private ShareListItem item;
    private ListImageAdapter adapter;
    private ActionBarMenu barMenu;
    private GridView gridview;
    private AlertDialog alertDialog;
    private View dialogview;
    private String content;

    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.activity_share_detail);
        ButterKnife.bind(this);
    }

    @Override
    protected void afterView() {
        item = getIntent().getParcelableExtra("share_item");
        shareDetailTitle.setText(item.title);
        shareDetailAuthor.setText(item.username);
        shareDetailTime.setText(TimeUtils.getTime(item.time));
        shareDetailGoods.setText(item.shopname1);
        oneTitlePeriodText.setText(item.qishu);
        shareDetailNum.setText(item.gonumber);
        shareDetailCode.setText(item.user_code);
        shareDetailContent.setText(item.content);
        shareDetailRevealTime.setText(TimeUtils.getTime(item.end_time));
        adapter = new ListImageAdapter(this, item.photolist);
        shareDetailImageList.setAdapter(adapter);
        shareDetailImageList.bindLinearLayout();
        setTitle(R.string.title_activity_share_detail);
        int size = ShopCartManager.getInstance().getCartsSize();
        if (size > 0) {
            topCart.setImageResource(R.drawable.ic_menu_overflow_withnew);
        } else {
            topCart.setImageResource(R.drawable.ic_menu_overflow);
        }
        topCart.setVisibility(View.VISIBLE);
        topCart.setOnClickListener(this);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) topRefresh.getLayoutParams();
        layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.top_cart);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
        topRefresh.setLayoutParams(layoutParams);

        topRefresh.setImageResource(R.drawable.ic_share_real);
        topRefresh.setVisibility(View.VISIBLE);
        topRefresh.setOnClickListener(this);
        shareDetailAuthor.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (topCart == v) {
            showMenu();
        }
        if (v == topRefresh) {
            showShareDialog();
        }
    }


    private void showShareDialog() {
        String title = getResources().getString(R.string.share_title);
        title = String.format(title, getResources().getString(R.string.app_name));
        final UMImage image = new UMImage(this, item.photolist.get(0));

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("别人只用").
                append(item.gonumber).
                append("元就获得了").
                append(item.title).append("\"").append(item.content).append("\"");
        content = stringBuilder.toString();
        final String url = Constants.SHARE_URL + item.id;
        ShareUtils shareUtils = new ShareUtils();
        final String finalTitle = title;
        shareUtils.showShareDialog(this, new ShareUtils.shareItemClickLister() {
            @Override
            public void onClick(SHARE_MEDIA media) {
                if (media == SHARE_MEDIA.SINA) {
                    content = content + url;
                }
                new ShareAction(ShareDetailActivity.this)
                        .setPlatform(media)
                        .setCallback(umShareListener).withTitle(finalTitle).withMedia(image)
                        .withText(content).withTargetUrl(url)
                        .share();
            }
        });

    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA share_media) {
            showToastMessage("分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            showToastMessage("分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            showToastMessage("分享取消");
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }

    private void showMenu() {
        if (barMenu == null) {
            barMenu = new ActionBarMenu(this);
            barMenu.setActionMenuListener(new ActionBarMenu.ActionBarMenuClickListener() {
                @Override
                public void onItemClick(int index) {
                    ChangePageEvent pageEvent = new ChangePageEvent();
                    if (index == 0) {
                        pageEvent.item = 3;
                    } else {
                        pageEvent.item = 0;
                    }
                    EventBus.getDefault().post(pageEvent);
                    AppManager.getAppManager().getTopWith(RootAcitivity.class);
                }
            });
        }
        barMenu.showAsDropDown(topCart, -ScreenUtils.dpToPxInt(this, 80), 0);
    }
}
